import config
from machine import I2C,Pin
from ssd1306 import SSD1306_I2C
import utime
import menu

i2c = I2C(scl = Pin(config.pin_oled_scl), sda=Pin(config.pin_oled_sda))
oled = SSD1306_I2C(config.oled_width, config.oled_height, i2c)

button_down = Pin(config.pin_button_down, Pin.IN, Pin.PULL_UP)
button_click = Pin(config.pin_button_click, Pin.IN, Pin.PULL_UP)


app_cnt = 0
# 自动检索所有app文件
app_folder = 'apps'
app_files = [f for f in os.listdir(app_folder) if f.endswith('.py')]
# 创建App实例列表
apps = [None] * len(app_files)
apps_name = []
for i, file in enumerate(app_files):
    module_name = file[:-3]  # 移除 '.py'
    module = __import__(app_folder+"/"+module_name)
    apps[i] = module.App(module_name, i)
    apps_name.append(module_name)
    app_cnt+=1


appMenu = menu.Menu(apps_name)
choice = 0
while True:
    choice = appMenu.runMenu()
    
    apps[choice].runApp()
    while(button_click.value() == 0): pass
